package com.xthink.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xthink.common.result.AjaxResult;
import com.xthink.common.pojo.polo.PoolOrder;
import com.xthink.common.vo.pool.ListPoolOrderVo;
import com.xthink.web.mapper.PoolOrderMapper;
import com.xthink.web.service.PoolOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PoolOrderServiceImpl extends ServiceImpl<PoolOrderMapper, PoolOrder> implements PoolOrderService {

    @Autowired
    private PoolOrderMapper poolOrderMapper;

    //流量池详情池订单查询
    @Override
    public AjaxResult listPoolIdOrder(ListPoolOrderVo listPoolOrderVo) {

        Page<PoolOrder> page = new Page<>(listPoolOrderVo.getPage(), listPoolOrderVo.getSize());
        LambdaQueryWrapper<PoolOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PoolOrder::getPoolId, listPoolOrderVo.getPoolId());
        wrapper.like(StringUtils.isNotBlank(listPoolOrderVo.getOrderNumber())
                , PoolOrder::getOrderNumber
                , listPoolOrderVo.getOrderNumber());
        wrapper.between(
                listPoolOrderVo.getStartTime() != null || listPoolOrderVo.getEndTime() != null
                , PoolOrder::getCreateTime
                , listPoolOrderVo.getStartTime()
                , listPoolOrderVo.getEndTime()
        );
        Page<PoolOrder> poolOrderPage = poolOrderMapper.selectPage(page, wrapper);
        List<PoolOrder> records = poolOrderPage.getRecords();
        if (records.size()==0){
            return AjaxResult.error("未查到此订单或该流量池暂未生成订单");
        }
        return AjaxResult.success(records);
    }

    //订单池查询
    @Override
    public AjaxResult listPoolOrder(ListPoolOrderVo listPoolOrderVo) {
        Page<PoolOrder> page = new Page<>(listPoolOrderVo.getPage(), listPoolOrderVo.getSize());
        LambdaQueryWrapper<PoolOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(listPoolOrderVo.getOrderNumber())
                , PoolOrder::getOrderNumber
                , listPoolOrderVo.getOrderNumber());
        wrapper.between(
                listPoolOrderVo.getStartTime() != null || listPoolOrderVo.getEndTime() != null
                , PoolOrder::getCreateTime
                , listPoolOrderVo.getStartTime()
                , listPoolOrderVo.getEndTime()
        );
        Page<PoolOrder> poolOrderPage = poolOrderMapper.selectPage(page, wrapper);
        List<PoolOrder> records = poolOrderPage.getRecords();
        if (records.size()==0){
            return AjaxResult.error("未查到此订单或流量池暂未生成订单");
        }
        return AjaxResult.success(records);
    }
}
